﻿using DataHelper;
using SelfServiceWriteSystem.Mode;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HandyControlProject5
{
    /// <summary>
    /// MainList.xaml 的交互逻辑
    /// </summary>
    public partial class MainList : UserControl
    {
        MainWindow mainWindow;



        List<Project> Projects = new List<Project>();
        List<Department> Departments = new List<Department>();
        List<Subject> Subjects = new List<Subject>();

        List<ShowItem> ProjectsShowItems = new List<ShowItem>();
        List<ShowItem> DepartmentsShowItems = new List<ShowItem>();
        List<ShowItem> SubjectsShowItems = new List<ShowItem>();

        int ProjectPage = 1;
        int DepartmentPage = 1;
        int SubjectPage = 1;

        int ProjectPageTotal = 1;
        int DepartmentPageTotal = 1;
        int SubjectPageTotal = 1;

        int ProjectOnePageNumber = 10;
        int DepartmentOnePageNumber = 12;
        int SubjectOnePageNumber = 44;

        public void ChangePage0(ListBox listBox, TextBlock textBlock, ref List<Project> projects, ref List<ShowItem> showItems, int page, int OnePageNumber)
        {
            int total = (int)Math.Ceiling(projects.Count / (double)OnePageNumber);
            if (total <= 0) return;
            if (page >= total)
                page = total;
            textBlock.Text = "第  " + page.ToString() + " / " + total.ToString() + "  页";
            ProjectPageTotal = total;
            showItems.Clear();
            for (int i = (page - 1) * OnePageNumber; i < (page) * OnePageNumber; i++)
            {
                if (i < projects.Count)
                {
                    showItems.Add(new ShowItem
                    {
                        Index = i + 1,
                        Name = projects[i].Name,
                        tag = projects[i]
                    });
                }
            }
        }
        public void ChangePage1(ListBox listBox, TextBlock textBlock, ref List<Department> departments, ref List<ShowItem> showItems, int page, int OnePageNumber)
        {
            int total = (int)Math.Ceiling(departments.Count / (double)OnePageNumber);
            if (total <= 0) return;
            if (page >= total)
                page = total;
            textBlock.Text = "第  " + page.ToString() + " / " + total.ToString() + "  页";
            DepartmentPageTotal = total;
            showItems.Clear();
            for (int i = (page-1) * OnePageNumber; i < (page) * OnePageNumber; i++)
            {
                if (i < departments.Count)
                {
                    showItems.Add(new ShowItem
                    {
                        Index = i+1,
                        Name = departments[i].Name,
                        tag = departments[i]
                    });
                }
            }
        }
        public void ChangePage2(ListBox listBox, TextBlock textBlock, ref List<Subject> subjects, ref List<ShowItem> showItems, int page, int OnePageNumber)
        {
            int total = (int)Math.Ceiling(subjects.Count / (double)OnePageNumber);
            if (total <= 0) return;
            if (page >= total)
                page = total;
            textBlock.Text = "第  " + page.ToString() + " / " + total.ToString() + "  页";
            SubjectPageTotal = total;
            showItems.Clear();
            for (int i = (page-1) * OnePageNumber; i < page * OnePageNumber; i++)
            {
                if (i < subjects.Count)
                {
                    showItems.Add(new ShowItem
                    {
                        Index = i+1,
                        Name = subjects[i].Name,
                        Pic = AppDomain.CurrentDomain.BaseDirectory + subjects[i].Image,
                        tag = subjects[i]
                    });
                }
            }
        }



        public MainList(MainWindow _mainWindow)
        {
            int index = 0;
            InitializeComponent();
            mainWindow = _mainWindow;

            //热门事项列表
            Projects = DbHelper.GetHotProjects(DateTime.Now.AddDays(-7),5);
            if (TabControl.SelectedIndex == 0)
                ChangePage0(ProjectsList, pageTextBlock, ref Projects, ref ProjectsShowItems, ProjectPage, ProjectOnePageNumber);
            ProjectsList.ItemsSource = ProjectsShowItems;

            //部门列表
            Departments = DbHelper.GetDepartments();
            if(TabControl.SelectedIndex == 1)
                ChangePage1(DepartmentList, pageTextBlock, ref Departments, ref DepartmentsShowItems, DepartmentPage, DepartmentOnePageNumber);
            DepartmentList.ItemsSource = DepartmentsShowItems;

            //主题列表
            Subjects = DbHelper.GetSubjects();
            if (TabControl.SelectedIndex == 2)
                ChangePage2(DepartmentList, pageTextBlock, ref Subjects, ref SubjectsShowItems, SubjectPage, SubjectOnePageNumber);
            SubjectList.ItemsSource = SubjectsShowItems;
 
        }

        private void HotDocsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var project = ProjectsList.SelectedItem as ShowItem;
                mainWindow.docView.GetDoc((project.tag as Project).Id, project.Name,false);
                mainWindow.UserContent = mainWindow.docView;
            }
            catch (Exception ex)
            {
                //MessageBox.Show("对不起，服务异常！\n" + ex.Message, "出错了", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            ProjectsList.SelectedIndex = -1;
        }

        private void DepartmentList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var department = DepartmentList.SelectedItem as ShowItem;
                mainWindow.eventList.GetEventByDepartment((department.tag as Department).Id, department.Name);
                mainWindow.UserContent = mainWindow.eventList;
            }
            catch (Exception ex)
            {
                //MessageBox.Show("对不起，服务异常！\n" + ex.Message, "出错了", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            DepartmentList.SelectedIndex = -1;
        }

        private void SubjectList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var subject = SubjectList.SelectedItem as ShowItem;
                mainWindow.eventList.GetEventBySubject((subject.tag as Subject).Id, subject.Name);
                mainWindow.UserContent = mainWindow.eventList;
            }
            catch (Exception ex)
            {
                //MessageBox.Show("对不起，服务异常！\n" + ex.Message, "出错了", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            SubjectList.SelectedIndex = -1;
        }

        private void DepartmentList_MouseUp(object sender, MouseButtonEventArgs e)
        {
           
        }

        private void SubjectList_MouseUp(object sender, MouseButtonEventArgs e)
        {
           
        }

        private void HotDoc_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            

        }
        /// <summary>
        /// 选项卡切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (TabControl.SelectedIndex == 0)
                {
                    ChangePage0(ProjectsList, pageTextBlock, ref Projects, ref ProjectsShowItems, ProjectPage, ProjectOnePageNumber);
                    ProjectsList.Items.Refresh();
                }
                else if (TabControl.SelectedIndex == 1)
                {
                    ChangePage1(DepartmentList, pageTextBlock, ref Departments, ref DepartmentsShowItems, DepartmentPage, DepartmentOnePageNumber);
                    DepartmentList.Items.Refresh();
                }
                else if (TabControl.SelectedIndex == 2)
                {
                    ChangePage2(DepartmentList, pageTextBlock, ref Subjects, ref SubjectsShowItems, SubjectPage, SubjectOnePageNumber);
                    SubjectList.Items.Refresh();
                }
            }
            catch (Exception ex)
            { 
            }
        }

        #region 分页切换
        /// <summary>
        /// 到首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (TabControl.SelectedIndex == 0)
            {
                ProjectPage = 1;
                ChangePage0(ProjectsList, pageTextBlock, ref Projects, ref ProjectsShowItems, ProjectPage, ProjectOnePageNumber);
                ProjectsList.Items.Refresh();
            }
            else if(TabControl.SelectedIndex == 1)
            {
                DepartmentPage = 1;
                ChangePage1(DepartmentList, pageTextBlock, ref Departments, ref DepartmentsShowItems, DepartmentPage, DepartmentOnePageNumber);
                DepartmentList.Items.Refresh();
            }
            else if (TabControl.SelectedIndex == 2)
            {
                SubjectPage = 1;
                ChangePage2(DepartmentList, pageTextBlock, ref Subjects, ref SubjectsShowItems, SubjectPage, SubjectOnePageNumber);
                SubjectList.Items.Refresh();
            }
        }
        /// <summary>
        /// 前一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (TabControl.SelectedIndex == 0)
            {
                if(ProjectPage > 1) ProjectPage -= 1;
                ChangePage0(ProjectsList, pageTextBlock, ref Projects, ref ProjectsShowItems, ProjectPage, ProjectOnePageNumber);
                ProjectsList.Items.Refresh();
            }
            else if(TabControl.SelectedIndex == 1)
            {
                if(DepartmentPage > 1) DepartmentPage -= 1;
                ChangePage1(DepartmentList, pageTextBlock, ref Departments, ref DepartmentsShowItems, DepartmentPage, DepartmentOnePageNumber);
                DepartmentList.Items.Refresh();
            }
            else if (TabControl.SelectedIndex == 2)
            {
                if(SubjectPage > 1) SubjectPage -= 1;
                ChangePage2(DepartmentList, pageTextBlock, ref Subjects, ref SubjectsShowItems, SubjectPage, SubjectOnePageNumber);
                SubjectList.Items.Refresh();
            }
        }
        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            if (TabControl.SelectedIndex == 0)
            {
                if (ProjectPage < ProjectPageTotal) ProjectPage += 1;
                    ChangePage0(ProjectsList, pageTextBlock, ref Projects, ref ProjectsShowItems, ProjectPage, ProjectOnePageNumber);
                ProjectsList.Items.Refresh();
            }
            else if (TabControl.SelectedIndex == 1)
            {
                if (DepartmentPage < DepartmentPageTotal) DepartmentPage += 1;
                ChangePage1(DepartmentList, pageTextBlock, ref Departments, ref DepartmentsShowItems, DepartmentPage, DepartmentOnePageNumber);
                //DepartmentList.ItemsSource = DepartmentsShowItems;
                DepartmentList.Items.Refresh();
            }
            else if (TabControl.SelectedIndex == 2)
            {
                if (SubjectPage < SubjectPageTotal) SubjectPage += 1; 
                ChangePage2(DepartmentList, pageTextBlock, ref Subjects, ref SubjectsShowItems, SubjectPage, SubjectOnePageNumber);
                //SubjectList.ItemsSource = SubjectsShowItems;
                SubjectList.Items.Refresh();
            }
        }
        /// <summary>
        /// 到尾页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            if (TabControl.SelectedIndex == 0)
            {
                ProjectPage = ProjectPageTotal;
                ChangePage0(ProjectsList, pageTextBlock, ref Projects, ref ProjectsShowItems, ProjectPage, ProjectOnePageNumber);
                ProjectsList.Items.Refresh();
            }
            else if(TabControl.SelectedIndex == 1)
            {
                DepartmentPage = DepartmentPageTotal;
                ChangePage1(DepartmentList, pageTextBlock, ref Departments, ref DepartmentsShowItems, DepartmentPage, DepartmentOnePageNumber);
                DepartmentList.Items.Refresh();
            }
            else if (TabControl.SelectedIndex == 2)
            {
                SubjectPage = SubjectPageTotal;
                ChangePage2(DepartmentList, pageTextBlock, ref Subjects, ref SubjectsShowItems, SubjectPage, SubjectOnePageNumber);
                SubjectList.Items.Refresh();
            }
        }
        #endregion

        private void DepartmentList_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            
        }
    }
}
